/**
 *
 *      ___                       ___
 *     /\  \          ___        /\  \
 *    /::\  \        /\  \      /::\  \
 *   /:/\:\  \       \:\  \    /:/\ \  \
 *  /:/  \:\  \      /::\__\  _\:\~\ \  \
 * /:/__/ \:\__\  __/:/\/__/ /\ \:\ \ \__\
 * \:\  \  \/__/ /\/:/  /    \:\ \:\ \/__/
 *  \:\  \       \::/__/      \:\ \:\__\
 *   \:\  \       \:\__\       \:\/:/  /
 *    \:\__\       \/__/        \::/  /
 *     \/__/                     \/__/
 *
 * CIS - Clang Indexing Service is a Project Manager & Source code indexing service
 * created so as to simplify the indexing of C/C++ based source code which use
 * standardized build mechanisms.
 *
 **/

#include "../includes/indexserver.hxx"
#include "../includes/indexserverconn.hxx"

#include <iostream>
#include <Poco/Logger.h>
#include <Poco/FormattingChannel.h>
#include <Poco/PatternFormatter.h>
#include <Poco/Message.h>
#include <Poco/FileChannel.h>
#include <Poco/LogStream.h>
#include <Poco/Net/TCPServer.h>
#include <Poco/Net/TCPServerConnectionFactory.h>
#include <Poco/Logger.h>

using namespace Poco::Net;
using namespace Poco::Util;
using namespace cis;

const std::string IndexServer::subSystemName = "IndexServer";

namespace {
Poco::Logger& logger = Poco::Logger::get("IndexServer");
Poco::LogStream logstream(logger);
}

void IndexServer::initialize(Application &app)
{
    interface = app.config().getString("server.host");
    port = app.config().getInt64("server.port");
    logstream << "Starting IndexServer @ " << interface << ":" << port << std::endl;
    server = new Poco::Net::TCPServer(new Poco::Net::TCPServerConnectionFactoryImpl<IndexServerConn>, ServerSocket(port));
    server->start();
}

void IndexServer::uninitialize()
{
    logstream << "Stopping IndexServer @ " << interface << ":" << port << std::endl;
    server->stop();
    delete server;
}

IndexServer::~IndexServer()
{
}